/**
 * 
 */
package com.xpec.c4.service.module.schedule;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 多做一層去呼叫真正要執行的 runnable object ({@link #theRunnable}), 並對他做 try/catch 希望能抓出問題
 * 
 * @author TW15010
 *
 */
public class LogOnJobObjectException implements Runnable {
	private static final Log log = LogFactory.getLog(LogOnJobObjectException.class);

	private Runnable theRunnable;

	public LogOnJobObjectException(Runnable theRunnable) {
		super();
		this.theRunnable = theRunnable;
	}

	@Override
	public void run() {
		try {
			theRunnable.run();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				if (theRunnable instanceof JobObject) {
					JobObject jb = (JobObject) theRunnable;
					log.error("run timer task " + jb.getTaskTimer().getModuleServer().getClass().getSimpleName()
							+ " \\ " + jb.getTaskTimer().getClass().getSimpleName() + " fail.");
				}

				log.error("error in executing: " + theRunnable + ". It will no longer be run!", e);
			}
			throw new RuntimeException(e);
		}
	}
}